<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Default_Form_ValidateAccount{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $update = false, $data = null){
        
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateName = new Zend_Validate();
        $validatePassword = new Zend_Validate();
        $validateRePassword = new Zend_Validate();
        $validateEmail = new Zend_Validate();
        $validatePhoneNumber = new Zend_Validate();
        
        $optionEmail = array('table' => 'accounts', 'field' => 'email');
        
        $validateName->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 50)), true)
                        ->addValidator(new Zend_Validate_Alnum(true), true);
        
        if(!$update){
            $validatePassword->addValidator(new Zend_Validate_NotEmpty, true)
                            ->addValidator(new Zend_Validate_StringLength(array('min' => 6, 'max' => 30)));
            
            $validateRePassword->addValidator(new Zend_Validate_NotEmpty, true)
                            ->addValidator(new Zend_Validate_Identical($params['password']), true);
        }
        
        $validateEmail->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 255)), true)
                        ->addValidator(new Zend_Validate_EmailAddress(), true)
                        ->addValidator(new Zend_Validate_Db_NoRecordExists($optionEmail), true);
        
        $validatePhoneNumber->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 11)), true)
                        ->addValidator(new Zend_Validate_Digits(true), true);
        
        /*
        * Check Name
        */
        
        if(!$validateName->isValid($params['name'])){
            $this->_messageError['name'] = '<label id="name" class="required"><em>' . $translate->_('name') . current($validateName->getMessages()) . '</em></label>';
            $this->_arrData['name'] = '';
            $this->_isError = true;
        }else{
            $this->_messageError['name'] = '';
            $this->_arrData['name'] = $params['name'];
        }
        
        /*
        * Check Password
        */
        
        if(!$update){
            if(!$validatePassword->isValid($params['password'])){
                $this->_messageError['password'] = '<label id="phone_number" class="required"><em>' . $translate->_('password') . current($validatePassword->getMessages()) . '</em></label>';
                $this->_arrData['password'] = '';
                $this->_isError = true;
            }else{
                $this->_messageError['password'] = '';
                $this->_arrData['password'] = $params['password'];
            }
        }
        
        /*
        * Check Repassword
        */
        
        if(!$update){
            if(!$validateRePassword->isValid($params['repassword'])){
                $this->_messageError['repassword'] = '<label id="phone_number" class="required"><em>' . $translate->_('repassword') . current($validateRePassword->getMessages()) . '</em></label>';
                $this->_arrData['repassword'] = '';
                $this->_isError = true;
            }else{
                $this->_messageError['repassword'] = '';
                $this->_arrData['repassword'] = $params['repassword'];
            }
        }
        
        /*
        * Check Email
        */
        
        if(!$update){
            if(!$validateEmail->isValid($params['email'])){
                $this->_messageError['email'] = '<label id="email" class="required"><em>' . $translate->_('email') . current($validateEmail->getMessages()) . '</em></label>';
                $this->_isError = true;
                $this->_arrData['email'] = '';
            }else{
                $this->_messageError['email'] = '';
                $this->_arrData['email'] = $params['email'];
            }
        }else{
            $this->_arrData['email'] = $data['email'];
        }
        
        /*
        * Check Phone Number
        */
        
        if(!$validatePhoneNumber->isValid($params['phone_number'])){
            $this->_messageError['phone_number'] = '<label id="phone_number" class="required"><em>' . $translate->_('phone number') . current($validatePhoneNumber->getMessages()) . '</em></label>';
            $this->_arrData['phone_number'] = '';
            $this->_isError = true;
        }else{
            $this->_messageError['phone_number'] = '';
            $this->_arrData['phone_number'] = $params['phone_number'];
        }
        
        if(!$this->isError()){
            if(!$update){
                $this->_arrData['password'] = sha1($params['password']);
                unset($this->_arrData['repassword']);
                
                $date = new Zend_Date();
                $curDate = $date->get(Zend_Registry::get('date_format_insert'));
                $this->_arrData['created'] = $curDate;
                $this->_arrData['activated'] = false;
                $this->_arrData['status'] = false;
                $this->_arrData['point'] = 0;
            }
        }
        
        $this->_arrData['address'] = $params['address'];
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}